package com.oot.twitter.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.oot.twitter.GlobalData;
import com.oot.twitter.R;
import com.oot.twitter.info.Tweet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author tuanla
 *
 */
public class TweetsListAdapter extends ArrayAdapter<Tweet>	{
	private Context context;
	private int resourceId;
	private ArrayList<Tweet> tweets;
	
	public TweetsListAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
		super(context, textViewResourceId, tweets);
		this.context = context;
		this.resourceId = textViewResourceId;
		this.tweets = tweets;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(resourceId, parent, false);
		}
		
		Tweet tweet = tweets.get(position);
		if (tweet != null) {
			ImageView avatar = (ImageView)convertView.findViewById(R.id.timeline_avarta);
			TextView fullname = (TextView)convertView.findViewById(R.id.timeline_fullname);
			TextView username = (TextView)convertView.findViewById(R.id.timeline_username);
			TextView time = (TextView)convertView.findViewById(R.id.timeline_time);
			TextView content = (TextView)convertView.findViewById(R.id.timeline_content);
			ImageView refav = (ImageView)convertView.findViewById(R.id.timeline_refav);
			
			GlobalData.imageLoader.DisplayImageRoundedCorner(tweet.getAvatar(), avatar);
			fullname.setText(tweet.getFull_name());
			username.setText("@" + tweet.getUsername());
			
			SimpleDateFormat informat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			SimpleDateFormat outformat = new SimpleDateFormat("dd MMM yy");
			Date created_time = null;
			try {
				created_time = informat.parse(tweet.getCreated_time());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			long diff_time = System.currentTimeMillis() - created_time.getTime(); 
			if (diff_time < 60000) {
				diff_time = diff_time / 1000;
				time.setText(diff_time + "s");
			} else if (diff_time < 86400000) {
				diff_time = diff_time / 60000;
				time.setText(diff_time + "h");
			} else if (diff_time < 518400000) {
				diff_time = diff_time / 86400000;
				time.setText(diff_time + "d");
			} else {
				time.setText(outformat.format(created_time));
			}
			
			content.setText(tweet.getContent());
			
			int refavVal = Integer.parseInt(tweet.getRefav());
			switch (refavVal) {
			case 1:
				refav.setImageResource(R.drawable.ic_dogear_rt);
				break;
			case 10:
				refav.setImageResource(R.drawable.ic_dogear_fave);
				break;
			case 11:
				refav.setImageResource(R.drawable.ic_dogear_both);
				break;
			}
		}
		
		return convertView;
	}
}
